package com.jml; 
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * jwt 加密
 */
public class JWTDemo01 {
    public static void main(String[] args) {
        // jwt秘钥
        String signKey="jmlKey";
        // 创建jwt,得到的jwt可以通过base64解码获取头和payload的信息.
        // setId和setSubject可以理解为jwt存放的数据,claim存放自定义数据
        JwtBuilder jwtBuilder = Jwts.builder()
                //jti:唯一身份标识
                .setId("66")
                //sub:接收jwt的一方
                .setSubject("jml")
                .setIssuedAt(new Date())
                .claim("userName","jmluserName")
                // 设置签名值
                .signWith(SignatureAlgorithm.HS256, signKey);
        System.out.println(jwtBuilder.compact());
    }
}
/**
 *头部:
 * eyJhbGciOiJIUzI1NiJ9.
 *      payload:
 *      eyJqdGkiOiI2NiIsInN1YiI6ImptbCIsImlhdCI6MTYxNzAwNzA3OCwidXNlck5hbWUiOiJqbWwifQ.
 *          签名:
 *          k_rI9oU_2PoWNgu-hecD_OjdHGKq7rlScwVbTRauk04
 * eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NiIsInN1YiI6ImptbCIsImlhdCI6MTYxNzAwNzA3OCwidXNlck5hbWUiOiJqbWwifQ.k_rI9oU_2PoWNgu-hecD_OjdHGKq7rlScwVbTRauk04
 */